package Data;

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.HashSet;
import java.util.Set;

public class HTMLParser {

	/**
	 * function: Document input(File).
	 * <p/>
	 * This function will attempt to parse the HTML file that is received as a parameter input.
	 *
	 * @param inputFile
	 * @return htmlFile a parsed version of the current HTML file.

    public Document parseHTML(File inputFile) {
		Document htmlFile = null;
		try {
			htmlFile = Jsoup.parse(inputFile, "UTF-8"); //htmlFile opens and parses the current html file specified by the inputFile parameter variable.
        } catch (IOException e) {
            Logger.getLogger(HTMLParser.class.getName()).log(Level.SEVERE, null, e);
		} catch (StringIndexOutOfBoundsException e) {
			Logger.getLogger(HTMLParser.class.getName()).log(Level.SEVERE, null, e);
		}

		return htmlFile; //returns the parsed html file
    }*/

	/**
	 * function: ArrayList processHTML(Document).
	 * <p/>
	 * The purpose of this function is to locate the areas within the body section of the HTML file that contain text based paragraphs.
	 * <p/>
	 * The function receives the parsed HTML file from the parseHTML function.
	 * <p/>
	 * Once the paragraphs are found within the parsed HTML file, they are converted from HTML format to plaintext.
	 * <p/>
	 * An arrayList will hold the title of the HTML page, as well as the individual paragraphs in separate indexes.
	 *
     //* @param htmlFile
     * @return HTMLPlainText an arrayList that contains the plaintext format of the HTML page's title as well as the individual paragraphs in separate indexes

    public ArrayList<String> processHTML(Document htmlFile) {
		Elements body = htmlFile.select("body"); //this selects the body section of the current html file

		ArrayList<String> HTMLPlainText = new ArrayList<String>(); //this array list will hold plaintext format of the HTML page's title and paragraphs in seperate indexes.

		HTMLPlainText.add(htmlFile.title()); //this adds the title of the html page into the first index of the array list

		for (Element pBody : body) {
			String selector = ".entry-content div";
			Elements pTag = pBody.select(selector);/*pBody.getElementsByTag("p").parents(); /*this will acess the iterator and find the <p> tags within it. Once the <p> are
    found, the parent of the <p> which will be a <div> is stored as a whole within pTag.
    for (int pTagCount = 0; pTagCount < pTag.size(); pTagCount++) {
				Element p = pTag.get(pTagCount); //p holds the current <div> that contains a <p>
				String plainText = p.text(); //plainText strips all of the HTML within the <div><p> so that the stored value is a plaintext string
				
				if (plainText.length() != 0) //some <p> will be empty, if they are there is no need to store them in the ArrayList
				{
					HTMLPlainText.add(plainText); /*if the <p> is not empty, store it into the arrayList, 
                                                    index 0 will always be the title of the page
                                                    index 1 will always be the original post/first <p> tag
    index 2-x will always be the reply posts/second <p> tag
    pTagCount++; //increment the pTagCount so that the next <p> can be processed
				}

				pTag.parents().empty(); //delete the <p> from the iterator so that the next <p> that is selected for processing is the actual next <p>
			}
		}
		return HTMLPlainText; //return the ArrayList
    }*/


    public Set<Item> parseHTMLtoProducts(Document doc, String url) {
        Elements prodTitle = doc.select("div.prod_tit");
        Elements prodDesc = doc.select("div.product-name");
        Elements prodRef = doc.select("div.sku");
        Elements prodPrice = doc.select("div.product-essential span.price");


        Set<Item> items = new HashSet<Item>();
        for (int i = 0; i < prodRef.size(); i++) {
            Item item = new Item(prodTitle.get(i).text(), prodRef.get(i).text(), prodDesc.get(i).text(), Double.parseDouble(prodPrice.get(i).text().replace("€", "").replace(",", ".").replace((char) 160, (char) 32)), url);
            items.add(item);
        }
        return items;
    }
}